package org.lee.basic.algorithm;

import java.util.Arrays;

/**
 * @author jackielee
 * @version 1.0
 * @description NumDemo1
 * @date 2021/4/19
 */
public class NumDemo1 {
  public static void main(String[] args) {
    for (int i = 0; i < 10000; ++i) {
      if (isCompSqrt(i + 100) && isCompSqrt(i + 268)) { // 先加100为完全，再加168还是完全
        System.out.println(i);
        break;
      }
    }

    sortMp();
    int[] arr = new int[] {1, 5, 9, 6, 3, 2};
    sortMp(arr);
    Arrays.stream(arr).forEach(a -> System.out.print(a + " "));
    System.out.println();

    // 反推吃桃子，每天吃前一天的半+1
    taozi();

    lex(5);

    list();

    testFatorial(20);
    System.out.println();

    System.out.println("5!=" + fatorial2(5));
  }

  private static boolean isCompSqrt(int n) {
    for (int i = 0; i < Math.sqrt(n) + 1; ++i) {
      if (n == Math.pow(i, 2)) {
        return true;
      }
    }
    return false;
  }

  private static void sortMp() {
    for (int i = 1; i < 10; ++i) {
      for (int j = 1; j < i + 1; ++j) {
        System.out.print(j + "*" + i + "=" + i * j + " ");
      }
      System.out.println();
    }
  }

  private static void sortMp(int[] arr) {
    for (int i = 0; i < arr.length; ++i) {
      for (int j = 0; j < arr.length - i - 1; ++j) {
        if (arr[j] > arr[j + 1]) {
          int temp = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = temp;
        }
      }
    }
  }

  private static void taozi() {
    int count = 1;
    for (int i = 10; i > 0; --i) {
      count = 2 * count + 2;
    }
    System.out.println(count);
  }

  private static void lex(int n) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 2 * i; ++j) {
        if (j < n - i) {
          System.out.print(" ");
        }
        if (j >= n - i && j <= n + i) {
          System.out.print("*");
        }
      }
      System.out.println();
    }

    for (int i = 1; i < n; ++i) {
      System.out.print(" ");
      for (int j = 0; j < 2 * n - i; ++j) {
        if (j < i) {
          System.out.print(" ");
        }
        if (j >= i && j < 2 * n - i - 1) {
          System.out.print("*");
        }
      }
      System.out.println();
    }
  }

  private static void list() {
    double n1 = 1;
    double n2 = 1;
    double fraction = n1 / n2;
    double sum = 0;
    for (int i = 0; i < 20; ++i) {
      double t1 = n1;
      double t2 = n2;
      n1 = t1 + t2;
      n2 = t1;
      fraction = n1 / n2;
      sum += fraction;
      System.out.println(n1 + "/" + n2);
    }
    System.out.println(sum);
  }

  private static int fatorial(int n) {
    int mult = 1;
    for (int i = 1; i <= n; ++i) {
      mult *= i;
    }
    return mult;
  }

  private static void testFatorial(int n) {
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
      System.out.print(i + "!");
      if (i < n) {
        System.out.print("+");
      } else {
        System.out.print("=");
      }
      sum += fatorial(i);
    }
    System.out.print(sum);
  }

  private static int fatorial2(int n) {
    if (n == 1) {
      return 1;
    }
    return n * fatorial2(n - 1);
  }
}
